package com.buss.customer.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.customer.model.CustomerSource;
import com.buss.customer.service.CustomerSourceService;

@Controller
@RequestMapping("/admin/customersource/")
public class CustomerSourceController extends BaseController {

	@Autowired
	private CustomerSourceService sourceService;
	
	@Override
	@RequestMapping("page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("customersource/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@RequestMapping("{type}")
	public ModelAndView editPage(@PathVariable String type,Long id){
		ModelAndView view = new ModelAndView("customersource/edit");
		CustomerSource source = new CustomerSource();
		if(EDIT_PAGE.equals(type)){
			source = sourceService.loadSource(id);
		}
		view.addObject("type", type);
		view.addObject("source", source);
		return view;
	}
	
	@Override
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		sourceService.removeSource(ids);
		return SysStaticParam.resultMessage("删除往来单位来源信息成功!");
	}

	@RequestMapping("add")
	@ResponseBody
	public Map<String,Object> add(CustomerSource source,HttpServletRequest request){
		source.setCompanyId(getCompanyId(request));
		source = sourceService.saveSource(source);
		Map<String,Object> map = SysStaticParam.resultMessage("新增往来单位来源信息成功!");
		map.put("source", source);
		return map;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Map<String,Object> edit(CustomerSource source,HttpServletRequest request){
		source.setCompanyId(getCompanyId(request));
		sourceService.editSource(source);
		return SysStaticParam.resultMessage("修改往来单位来源信息成功!");
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> list(String name,@ModelAttribute Pagination page,HttpServletRequest request){
		ResultList<CustomerSource> rs = sourceService.querySource(getCompanyId(request), name, page);
		Map<String,Object> map = SysStaticParam.resultMessage("获取往来单位信息列表成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
}
